<?php

/*
 * This file will perform the ajax for getting the positions for the search results.
 * This is being used by admPositionSetup.php.
 * 
 * Created by: Peter Agno Jr.
 * Date created: November 21, 2011
 * 
 * In parameters: posId, posName, and deptName
 * Out parameters: position information for search results
 */

session_start();

// Start - Checker for those users who will just go to the page by typing directly in the url.
if ($_POST) {
    include('../includes/siteConfig.php');
    
    // connect to database
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');
    
    // Get the data from post
    $posId = $_POST['posId'];
    $posName = $_POST['posName'];
    $deptName = $_POST['deptName'];
    
    // Prepare filter for posId
    $filterPos = "";
    if ( $posId != "" ) {
        $filterPos = "WHERE positionId LIKE '%" . $posId . "%' ";
    }
    
    // Prepare filter for posName
    if ( $posName != "" ) {
        if ( $filterPos == "" ) {
            $filterPos = "WHERE ";
        }
        else {
            $filterPos = $filterPos . "AND ";
        }
        
        $filterPos = $filterPos . "positionName LIKE '%" . $posName . "%' ";
    }
    
    // Prepare filter for deptName
    if ( $deptName != "" ) {
        if ( $filterPos == "" ) {
            $filterPos = "WHERE ";
        }
        else {
            $filterPos = $filterPos . "AND ";
        }
        
        $filterPos = $filterPos . "departmentName LIKE '%" . $deptName . "%' ";
    }
    
    // Prepare the query for getting the departments
//    $query =
//        "
//            -- Filter department and positions
//            SELECT *
//            FROM
//            
//                -- Select all departments and positions
//                (SELECT P.positionId as positionId, P.positionName as positionName, D.departmentName as departmentName
//                FROM Department as D, Position as P, Department_Position as DP
//                WHERE D.departmentId = DP.departmentId AND P.positionId = DP.positionId) as Dept_Pos
//                
//            ".$filterPos."
//                
//            ORDER BY positionName ASC, departmentName ASC
//        ";
    
    // Prepare the query for getting the departments
    $query =
        "
            -- Filter department and positions
            SELECT *
            FROM Position                
            ".$filterPos."
            GROUP BY positionId    
            ORDER BY positionId ASC
        ";
    $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    
    // Identify if there were querried departments
    if ( mysql_num_rows($result) > 0 ) {
        while ($row = mysql_fetch_array($result)) {
            $jsondata = array();
            $jsondata['posId'] = $row['positionId'];
            $jsondata['posName'] = $row['positionName'];
//            $jsondata['deptName'] = $row['departmentName'];
            $feed[] = $jsondata;
        }
        
        echo json_encode($feed);
    }
    else {
        echo json_encode(0);
    }
    
    mysql_free_result($result);
    mysql_close($connect);
}   // End - Checker for those users who will just go to the page by typing directly in the url.
else {
    echo "You are not authorized to view this page. This incident will be reported immediately.";
}
?>
